
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Operator · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="filter.html" />
    
    
    <link rel="prev" href="cp.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    快速入门
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../guides/project.html">
            
                <a href="../guides/project.html">
            
                    
                    项目结构和初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../guides/api.html">
            
                <a href="../guides/api.html">
            
                    
                    API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../guides/doc.html">
            
                <a href="../guides/doc.html">
            
                    
                    生成 API 文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../guides/client.html">
            
                <a href="../guides/client.html">
            
                    
                    生成客户端
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="cp.html">
            
                <a href="cp.html">
            
                    
                    Consumer && Producer
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.2" data-path="operator.html">
            
                <a href="operator.html">
            
                    
                    Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="filter.html">
            
                <a href="filter.html">
            
                    
                    Filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="prefab.html">
            
                <a href="prefab.html">
            
                    
                    Prefab
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="modifier.html">
            
                <a href="modifier.html">
            
                    
                    Modifier
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="middleware.html">
            
                <a href="middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="context.html">
            
                <a href="context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="source.html">
            
                <a href="source.html">
            
                    
                    Parameter Generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="destination.html">
            
                <a href="destination.html">
            
                    
                    Destination Handler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="error.html">
            
                <a href="error.html">
            
                    
                    错误包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="method.html">
            
                <a href="method.html">
            
                    
                    方法包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="configurer.html">
            
                <a href="configurer.html">
            
                    
                    配置器机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="plugin.html">
            
                <a href="plugin.html">
            
                    
                    插件机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="clients.html">
            
                <a href="clients.html">
            
                    
                    多客户端整合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    插件
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../plugins/logger.html">
            
                <a href="../plugins/logger.html">
            
                    
                    系统日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../plugins/reqlog.html">
            
                <a href="../plugins/reqlog.html">
            
                    
                    请求日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../plugins/metrics.html">
            
                <a href="../plugins/metrics.html">
            
                    
                    监控指标插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../plugins/tracing.html">
            
                <a href="../plugins/tracing.html">
            
                    
                    请求追踪插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../plugins/profiling.html">
            
                <a href="../plugins/profiling.html">
            
                    
                    性能分析插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../plugins/version.html">
            
                <a href="../plugins/version.html">
            
                    
                    版本信息插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../plugins/healthcheck.html">
            
                <a href="../plugins/healthcheck.html">
            
                    
                    健康检查插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    框架开发者指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../topics/start.html">
            
                <a href="../topics/start.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../topics/log.html">
            
                <a href="../topics/log.html">
            
                    
                    log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../topics/errors.html">
            
                <a href="../topics/errors.html">
            
                    
                    errors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../topics/definition.html">
            
                <a href="../topics/definition.html">
            
                    
                    definition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../topics/method.html">
            
                <a href="../topics/method.html">
            
                    
                    definition/method
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../topics/converter.html">
            
                <a href="../topics/converter.html">
            
                    
                    operators/converter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../topics/validator.html">
            
                <a href="../topics/validator.html">
            
                    
                    operators/validator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="../topics/router.html">
            
                <a href="../topics/router.html">
            
                    
                    service/router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="../topics/service.html">
            
                <a href="../topics/service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="../topics/nirvana.html">
            
                <a href="../topics/nirvana.html">
            
                    
                    nirvana
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="../topics/config.html">
            
                <a href="../topics/config.html">
            
                    
                    config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="../topics/rest.html">
            
                <a href="../topics/rest.html">
            
                    
                    rest
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="../topics/utils.html">
            
                <a href="../topics/utils.html">
            
                    
                    utils
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="../topics/command.html">
            
                <a href="../topics/command.html">
            
                    
                    cmd/nirvana
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Operator</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="operator">Operator</h1>
<p>&#x5305;&#x8DEF;&#x5F84;: <code>github.com/caicloud/nirvana/definition</code></p>
<p>&#x5728;&#x6BCF;&#x4E2A; API Definition &#x4E2D;&#xFF0C;&#x90FD;&#x6709;&#x4E00;&#x7EC4; Parameters &#x548C; Results&#x3002;&#x5176;&#x4E2D; Parameters &#x548C;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4E00;&#x4E00;&#x5BF9;&#x5E94;&#xFF0C;&#x800C; Results &#x5219;&#x548C;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4E00;&#x4E00;&#x5BF9;&#x5E94;&#x3002;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5BF9;&#x53C2;&#x6570;&#x7684;&#x5408;&#x6CD5;&#x6027;&#x548C;&#x7ED3;&#x6784;&#x8FDB;&#x884C;&#x9A8C;&#x8BC1;&#x548C;&#x8F6C;&#x6362;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x9700;&#x8981;&#x5728;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x4E2D;&#x5B9E;&#x73B0;&#x8FD9;&#x4E9B;&#x529F;&#x80FD;&#x3002;&#x4E3A;&#x4E86;&#x8BA9;&#x6570;&#x636E;&#x9A8C;&#x8BC1;&#x548C;&#x7ED3;&#x6784;&#x8F6C;&#x6362;&#x7B49;&#x903B;&#x8F91;&#x66F4;&#x52A0;&#x901A;&#x7528;&#x5316;&#x548C;&#x6807;&#x51C6;&#x5316;&#xFF0C;Nirvana &#x63D0;&#x4F9B;&#x4E86; Operator &#x63A5;&#x53E3;&#xFF0C;&#x53EF;&#x7528;&#x4E8E;&#x9488;&#x5BF9;&#x5355;&#x4E2A;&#x53C2;&#x6570;&#x6216;&#x8FD4;&#x56DE;&#x503C;&#x8FDB;&#x884C;&#x9A8C;&#x8BC1;&#x548C;&#x4FEE;&#x6539;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Operator is used to operate an object and return an replacement object.</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// For example:</span>
<span class="hljs-comment">//  A converter:</span>
<span class="hljs-comment">//    type ConverterForAnObject struct{}</span>
<span class="hljs-comment">//    func (c *ConverterForAnObject) Kind() {return &quot;converter&quot;}</span>
<span class="hljs-comment">//    func (c *ConverterForAnObject) In() reflect.Type {return definition.TypeOf(&amp;ObjectV1{})}</span>
<span class="hljs-comment">//    func (c *ConverterForAnObject) Out() reflect.Type {return definition.TypeOf(&amp;ObjectV2{})}</span>
<span class="hljs-comment">//    func (c *ConverterForAnObject) Operate(ctx context.Context, object interface{}) (interface{}, error) {</span>
<span class="hljs-comment">//        objV2, err := convertObjectV1ToObjectV2(object.(*ObjectV1))</span>
<span class="hljs-comment">//        return objV2, err</span>
<span class="hljs-comment">//    }</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">//  A validator:</span>
<span class="hljs-comment">//    type ValidatorForAnObject struct{}</span>
<span class="hljs-comment">//    func (c *ValidatorForAnObject) Kind() {return &quot;validator&quot;}</span>
<span class="hljs-comment">//    func (c *ValidatorForAnObject) In() reflect.Type {return definition.TypeOf(&amp;Object{})}</span>
<span class="hljs-comment">//    func (c *ValidatorForAnObject) Out() reflect.Type {return definition.TypeOf(&amp;Object{})}</span>
<span class="hljs-comment">//    func (c *ValidatorForAnObject) Operate(ctx context.Context, object interface{}) (interface{}, error) {</span>
<span class="hljs-comment">//        if err := validate(object.(*Object)); err != nil {</span>
<span class="hljs-comment">//            return nil, err</span>
<span class="hljs-comment">//        }</span>
<span class="hljs-comment">//        return object, nil</span>
<span class="hljs-comment">//    }</span>
<span class="hljs-keyword">type</span> Operator <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// Kind indicates operator type.</span>
    Kind() <span class="hljs-keyword">string</span>
    <span class="hljs-comment">// In returns the type of the only object parameter of operator.</span>
    <span class="hljs-comment">// The type must be a concrete struct or built-in type. It should</span>
    <span class="hljs-comment">// not be interface unless it&apos;s a file or stream reader.</span>
    In() reflect.Type
    <span class="hljs-comment">// Out returns the type of the only object result of operator.</span>
    <span class="hljs-comment">// The type must be a concrete struct or built-in type. It should</span>
    <span class="hljs-comment">// not be interface unless it&apos;s a file or stream reader.</span>
    Out() reflect.Type
    <span class="hljs-comment">// Operate operates an object and return one.</span>
    Operate(ctx context.Context, field <span class="hljs-keyword">string</span>, object <span class="hljs-keyword">interface</span>{}) (<span class="hljs-keyword">interface</span>{}, error)
}
</code></pre>
<p>&#x5728;&#x6CA1;&#x6709; Operator &#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;Nirvana &#x4ECE;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4E2D;&#x786E;&#x5B9A;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x5E76;&#x5C06;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x8F6C;&#x6362;&#x4E3A;&#x8FD9;&#x4E2A;&#x7C7B;&#x578B;&#x3002;&#x4F46;&#x662F;&#x5982;&#x679C;&#x8BBE;&#x7F6E;&#x4E86; Operator&#xFF0C;&#x90A3;&#x4E48; Nirvana &#x4F1A;&#x4ECE;&#x7B2C;&#x4E00;&#x4E2A; Operator &#x7684; <code>In()</code> &#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x7C7B;&#x578B;&#xFF0C;
&#x5E76;&#x4E14;&#x4F1A;&#x68C0;&#x67E5;&#x6700;&#x540E;&#x4E00;&#x4E2A; Operator &#x7684; <code>Out()</code> &#x7C7B;&#x578B;&#x662F;&#x5426;&#x548C;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x4E00;&#x81F4;&#x3002;</p>
<p>&#x5728;&#x5B9E;&#x9645;&#x7684;&#x4F7F;&#x7528;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x590D;&#x6742;&#x7684;&#x63A5;&#x53E3;&#x3002;Nirvana &#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#x7684; Operator&#xFF1A;Validator &#x548C; Converter&#x3002;</p>
<h2 id="validator">Validator</h2>
<p>&#x5305;&#x8DEF;&#x5F84;: <code>github.com/caicloud/nirvana/operators/validator</code></p>
<p>validator &#x5305;&#x7684;&#x5B9E;&#x73B0;&#x57FA;&#x4E8E; <a href="https://godoc.org/gopkg.in/go-playground/validator.v9" target="_blank">go-playground/validator</a>&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x7528;&#x4E8E;&#x751F;&#x6210; Operator &#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> Struct(instance <span class="hljs-keyword">interface</span>{}) Validator
<span class="hljs-keyword">func</span> String(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Int(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Int64(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Int32(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Int16(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Int8(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Byte(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Uint(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Uint64(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Uint32(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Uint16(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Uint8(tag <span class="hljs-keyword">string</span>) Validator
<span class="hljs-keyword">func</span> Bool(tag <span class="hljs-keyword">string</span>) Validator
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;&#xFF0C;&#x5728;&#x9700;&#x8981;&#x7684;&#x5B57;&#x6BB5;&#x4E0A;&#x6DFB;&#x52A0;&#x540D;&#x4E3A; <code>validate</code> &#x7684; tag&#x3002;</p>
<h3 id="&#x81EA;&#x5B9A;&#x4E49;&#x9A8C;&#x8BC1;&#x5668;">&#x81EA;&#x5B9A;&#x4E49;&#x9A8C;&#x8BC1;&#x5668;</h3>
<p>&#x6709;&#x65F6;&#x5019;&#x9ED8;&#x8BA4;&#x7684;&#x9A8C;&#x8BC1;&#x5668;&#x4E0D;&#x80FD;&#x8986;&#x76D6;&#x590D;&#x6742;&#x7684;&#x9A8C;&#x8BC1;&#x9700;&#x6C42;&#xFF0C;&#x56E0;&#x6B64; validator &#x5305;&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x81EA;&#x5B9A;&#x4E49;&#x9A8C;&#x8BC1;&#x5668;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// NewCustom calls f for validation, using description for doc gen.</span>
<span class="hljs-comment">// User should only do custom validation in f.</span>
<span class="hljs-comment">// Validations which can be done by other way should be done in another Operator.</span>
<span class="hljs-comment">// Exp:</span>
<span class="hljs-comment">// []definition.Operator{NewCustom(f,&quot;custom validation description&quot;)}</span>
<span class="hljs-comment">// f should be func(ctx context.Context, object AnyType) error</span>
<span class="hljs-keyword">func</span> NewCustom(f <span class="hljs-keyword">interface</span>{}, description <span class="hljs-keyword">string</span>) Validator
</code></pre>
<p>&#x9A8C;&#x8BC1;&#x5668;&#x51FD;&#x6570;&#x5FC5;&#x987B;&#x7B26;&#x5408;&#x7B7E;&#x540D; <code>func(ctx context.Context, object AnyType) error</code>&#x3002;&#x5176;&#x4E2D; AnyType &#x662F;&#x5177;&#x4F53;&#x8981;&#x9A8C;&#x8BC1;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x63A5;&#x53E3;&#x3002;</p>
<h2 id="converter">Converter</h2>
<p>&#x5305;&#x8DEF;&#x5F84;: <code>github.com/caicloud/nirvana/operators/converter</code></p>
<p>&#x9664;&#x4E86;&#x5BF9;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x9A8C;&#x8BC1;&#x4EE5;&#x5916;&#xFF0C;&#x5728;&#x67D0;&#x4E9B;&#x573A;&#x666F;&#x4E0B;&#x8FD8;&#x9700;&#x8981;&#x5BF9;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x3002;&#x56E0;&#x6B64; converter &#x5305;&#x63D0;&#x4F9B;&#x4E86;&#x5DE5;&#x5177;&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x5C06;&#x8F6C;&#x6362;&#x51FD;&#x6570;&#x5305;&#x88C5;&#x6210; Operator&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// For creates converter for a converter func.</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// A converter func should has signature:</span>
<span class="hljs-comment">//  func f(context.Context, string, AnyType) (AnyType, error)</span>
<span class="hljs-comment">// The second parameter is a string that is used to generate error.</span>
<span class="hljs-comment">// AnyType can be any type in go. But struct type and</span>
<span class="hljs-comment">// built-in data type is recommended.</span>
<span class="hljs-keyword">func</span> For(f <span class="hljs-keyword">interface</span>{}) Converter
</code></pre>
<p>&#x8F6C;&#x6362;&#x51FD;&#x6570;&#x5FC5;&#x987B;&#x7B26;&#x5408; <code>func f(context.Context, string, AnyType) (AnyType, error)</code>&#x3002;&#x5176;&#x4E2D;&#x53C2;&#x6570;&#x7684; AnyType &#x548C;&#x8FD4;&#x56DE;&#x503C;&#x7684; AnyType &#x53EF;&#x4EE5;&#x4E0D;&#x540C;&#x3002;</p>
<h2 id="&#x5728;-definition-&#x4E2D;&#x4F7F;&#x7528;-operator">&#x5728; Definition &#x4E2D;&#x4F7F;&#x7528; Operator</h2>
<p>&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5728; List Messages &#x7684; API &#x4E2D;&#x6DFB;&#x52A0; Operator &#x7684;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Definition</span>
<span class="hljs-keyword">var</span> listMessages = def.Definition{
    Method:      def.List,
    Summary:     <span class="hljs-string">&quot;List Messages&quot;</span>,
    Description: <span class="hljs-string">&quot;Query a specified number of messages and returns an array&quot;</span>,
    Function:    message.ListMessages,
    Parameters: []def.Parameter{
        {
            Source:  def.Query,
            Name:    <span class="hljs-string">&quot;count&quot;</span>,
            Default: <span class="hljs-number">10</span>,
            Operators: []def.Operator{
                validator.Int(<span class="hljs-string">&quot;min=1&quot;</span>),
                converter.For(<span class="hljs-keyword">func</span>(ctx context.Context, field <span class="hljs-keyword">string</span>, value <span class="hljs-keyword">int</span>) (<span class="hljs-keyword">uint</span>, error) {
                    <span class="hljs-keyword">return</span> <span class="hljs-keyword">uint</span>(value), <span class="hljs-literal">nil</span>
                }),
            },
            Description: <span class="hljs-string">&quot;Number of messages&quot;</span>,
        },
    },
    Results: def.DataErrorResults(<span class="hljs-string">&quot;A list of messages&quot;</span>),
}

<span class="hljs-comment">// &#x4E1A;&#x52A1;&#x51FD;&#x6570;</span>
<span class="hljs-comment">// ListMessages returns all messages.</span>
<span class="hljs-keyword">func</span> ListMessages(ctx context.Context, count <span class="hljs-keyword">uint</span>) ([]Message, error) {
    messages := <span class="hljs-built_in">make</span>([]Message, count)
    <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; <span class="hljs-keyword">int</span>(count); i++ {
        messages[i].ID = i
        messages[i].Title = fmt.Sprintf(<span class="hljs-string">&quot;%s %d&quot;</span>, m.Title, i)
        messages[i].Content = fmt.Sprintf(<span class="hljs-string">&quot;%s %d&quot;</span>, m.Content, i)
    }
    <span class="hljs-keyword">return</span> messages, <span class="hljs-literal">nil</span>
}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x9A8C;&#x8BC1;&#x5668;&#x8981;&#x6C42; count &#x7684;&#x6700;&#x5C0F;&#x503C;&#x4E3A; 1&#xFF0C;&#x5E76;&#x4E14;&#x628A; int &#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x4E3A;&#x4E86; uint &#x7C7B;&#x578B;&#x3002;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4E5F;&#x54CD;&#x5E94;&#x7684;&#x53D8;&#x6210;&#x4E86; uint &#x7C7B;&#x578B;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF1A;Operator &#x662F;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4E0A;&#x4E00;&#x4E2A; Operator &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F1A;&#x4F5C;&#x4E3A;&#x4E0B;&#x4E00;&#x4E2A; Operator &#x7684;&#x53C2;&#x6570;&#x3002;&#x56E0;&#x6B64;&#x5982;&#x679C;&#x5C06;&#x9A8C;&#x8BC1;&#x5668;&#x548C;&#x8F6C;&#x6362;&#x5668;&#x7684;&#x987A;&#x5E8F;&#x8C03;&#x6362;&#xFF0C;&#x5219;&#x9700;&#x8981;&#x5C06;&#x9A8C;&#x8BC1;&#x5668;&#x7684;&#x7C7B;&#x578B;&#x4FEE;&#x6539;&#x4E3A;&#x8F6C;&#x6362;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-go">Operators: []def.Operator{
    converter.For(<span class="hljs-keyword">func</span>(ctx context.Context, field <span class="hljs-keyword">string</span>, value <span class="hljs-keyword">int</span>) (<span class="hljs-keyword">uint</span>, error) {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">uint</span>(value), <span class="hljs-literal">nil</span>
    }),
    validator.Uint(<span class="hljs-string">&quot;min=1&quot;</span>),
},
</code></pre>
<p>&#x4F46;&#x662F;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x59CB;&#x7EC8;&#x5EFA;&#x8BAE;&#x9A8C;&#x8BC1;&#x5668;&#x653E;&#x5728;&#x524D;&#x9762;&#xFF0C;&#x8F6C;&#x6362;&#x5668;&#x653E;&#x5728;&#x540E;&#x9762;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="cp.html" class="navigation navigation-prev " aria-label="Previous page: Consumer && Producer">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="filter.html" class="navigation navigation-next " aria-label="Next page: Filter">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Operator","level":"1.3.2","depth":2,"next":{"title":"Filter","level":"1.3.3","depth":2,"path":"concepts/filter.md","ref":"concepts/filter.md","articles":[]},"previous":{"title":"Consumer && Producer","level":"1.3.1","depth":2,"path":"concepts/cp.md","ref":"concepts/cp.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*"},"file":{"path":"concepts/operator.md","mtime":"2019-08-06T09:48:58.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-20T10:33:16.586Z"},"basePath":"..","book":{"language":"zh-hans"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

